package ar.com.solidussource.howmuchmobile.activities.list;

import android.app.Activity;
import android.content.Intent;
import android.location.Address;
import android.location.Geocoder;
import android.os.Bundle;
import android.widget.Toast;
import ar.com.solidussource.howmuchmobile.activities.generic.PaginatedGenListActivity;
import ar.com.solidussource.howmuchmobile.common.exception.ServiceException;
import ar.com.solidussource.howmuchmobile.services.ProductsubcategoryRestService;

import java.io.IOException;
import java.util.Iterator;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: FT31270775
 * Date: 26/06/13
 * Time: 17:10
 * To change this template use File | Settings | File Templates.
 */
public class AddressListActivity extends PaginatedGenListActivity<Address> {
    @Override
    public String getViewTitle() {
        return "Direcciones";
    }

    @Override
    public void serviceCall() throws ServiceException {
        String addressName = getIntent().getExtras().getString("addressName");

        Geocoder geoCoder = new Geocoder(getBaseContext());

        List<Address> addressList = null;
        try{
            addressList = geoCoder.getFromLocationName(addressName, 5);
        } catch (IOException e){
            e.printStackTrace();
        }

        data.addAll(addressList);
    }

    @Override
    public Integer getElementId(int position) {
        return 0;
    }

    @Override
    public String getElementName(int position) {

        Address address = data.get(position);
        String addressLine = "";

        int i = 0;
        while (i < address.getMaxAddressLineIndex()){
            addressLine += address.getAddressLine(i) +" ";
            i++;
        }

        return addressLine;
    }

    @Override
    public void onElementClick(int position) {
        //Intent intent = new Intent(ProductsubcategoryListActivity.this, ProductListActivity.class);
        //intent.putExtra("productsubcategoryId", data.get(position).getId());
        //intent.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
        //startActivity(intent);
    }
}